/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import fse from 'fs-extra';
import { resolve } from 'path';

import { getJson5Obj } from '../../src/utils/json-util';

import { initByStageEmptyApp, initExecuteCommand, runHvigor, } from './common/common';
import { ModuleBuildProfileModifier } from './common/modifier/module-build-profile-modifier';
import { Probes } from './common/probes';

const compileResourcesTask = 'default@CompileResource';

describe('test compile resources', () => {
  test.concurrent('test stage app compile resource', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        tasks: compileResourcesTask
      });
      commands.push('--stacktrace');
      let result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      // 更新之后需要刷新生成的main_pages
      const moduleBuildProfileModifier = new ModuleBuildProfileModifier(projectRoot, 'entry');
      moduleBuildProfileModifier.updateTarget({
        name: 'default', source: {
          pages: [
            'pages/page'
          ]
        }
      }).outputSync();

      result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished :entry:default@CompileResource')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const probes = new Probes(projectRoot, resolve(projectRoot, 'entry'));
      const mainPageJson = resolve(probes.getIntermediatesResProfilePath(), 'main_pages.json');
      expect(getJson5Obj(mainPageJson)).toEqual({
        src: [
          'pages/page'
        ]
      });
      const resourceTablePath = resolve(probes.getIntermediatesRes(), 'ResourceTable.txt');
      let resourceIds = fse.readFileSync(resourceTablePath).toString();
      expect(resourceIds.includes('profile main_pages 0x0100000c')).toBeTruthy();
      const stringJsonPath = resolve(projectRoot, 'entry/src/main/resources/base/element/string.json');
      const stringRes = fse.readJsonSync(stringJsonPath);
      stringRes.string.push({
        name: 'module_desc2',
        value: 'module description'
      });
      fse.outputJSONSync(stringJsonPath, stringRes);
      result = runHvigor(projectRoot, commands);
      resourceIds = fse.readFileSync(resourceTablePath).toString();

      // 检验旧的资源id是否保持不变
      // ---暂时注释 仅api10 sdk支持
      // expect(resourceIds.includes('profile main_pages 0x0100000c')).toBeTruthy();
    });
  });
});

const previewCompileResourcesTask = 'default@PreviewCompileResource';
const compileResInc = 'compileResInc=true';

// Restool增量方式资源编译
describe('test increment compile resources', () => {
  test.concurrent('test stage app compile resource', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        tasks: previewCompileResourcesTask,
        compileResInc: compileResInc
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    });
  });
});
